package com.ruoyi.web.controller.theme;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.resource.service.IFileOperator;
import com.ruoyi.theme.domain.ThemeInfo;
import com.ruoyi.theme.domain.ThemeInfoVO;
import com.ruoyi.theme.service.IThemeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 主题管理Controller
 * 
 * @author ruoyi
 * @date 2023-05-17
 */
@RestController
@RequestMapping("/themeManager/info")
public class ThemeInfoControllerV2 extends BaseController
{
    @Autowired
    private IThemeInfoService themeInfoService;

    @Autowired
    IFileOperator iFileOperator;
    /**
     * 查询主题管理列表
     */
    @PreAuthorize("@ss.hasPermi('themeManager:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ThemeInfo themeInfo)
    {
        startPage();
        //转成前端字符串
        List<ThemeInfoVO> list = themeInfoService.selectThemeInfoList(themeInfo);
        return getDataTable(list);
    }

//    /**
//     * 导出主题管理列表
//     */
//    @PreAuthorize("@ss.hasPermi('themeManager:info:export')")
//    @Log(title = "主题管理", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ThemeInfo themeInfo)
//    {
//        List<ThemeInfo> list = themeInfoService.selectThemeInfoList(themeInfo);
//        ExcelUtil<ThemeInfo> util = new ExcelUtil<ThemeInfo>(ThemeInfo.class);
//        util.exportExcel(response, list, "主题管理数据");
//    }

    /**
     * 获取主题管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('themeManager:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(themeInfoService.selectThemeInfoById(id));
    }

    /**
     * 新增主题管理
     */
    @PreAuthorize("@ss.hasPermi('themeManager:info:add')")
    @Log(title = "主题管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ThemeInfo themeInfo)
    {
        return toAjax(themeInfoService.insertThemeInfo(themeInfo));
    }

    /**
     * 修改主题管理
     */
    @PreAuthorize("@ss.hasPermi('themeManager:info:edit')")
    @Log(title = "主题管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ThemeInfo themeInfo)
    {
        return toAjax(themeInfoService.updateThemeInfo(themeInfo));
    }

    /**
     * 删除主题管理
     */
    @PreAuthorize("@ss.hasPermi('themeManager:info:remove')")
    @Log(title = "主题管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        //把前端的东西删除
        Integer[] idRows = new Integer[ids.length];
        for(int i = 0 ;i<ids.length; i++) {
            //第几屏
            idRows[i] = Integer.parseInt(ids[i].substring(1,2));

        }
        //需要把本地文件删除
        for(Integer item : idRows){
            iFileOperator.DeleteThemeByScreenId(item);
        }
        return toAjax(themeInfoService.deleteThemeInfoByIds(idRows));
    }


}
